package com.docker.drug.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.docker.drug.domain.DockerDrug;
import com.docker.drug.domain.DoctorDrugWarehousingIntermediate;
import com.docker.drug.mapper.DockerDrugMapper;
import com.docker.drug.mapper.DoctorDrugWarehousingIntermediateMapper;
import com.docker.drug.mapper.DoctorWarehousingMapper;
import com.docker.drug.domain.DoctorWarehousing;
import com.docker.drug.service.IDoctorWarehousingService;

/**
 * 药品入库Service业务层处理
 * 
 * @author lin
 * @date 2025-04-10
 */
@Service
public class DoctorWarehousingServiceImpl implements IDoctorWarehousingService 
{
    @Autowired
    private DoctorWarehousingMapper doctorWarehousingMapper;
    @Autowired
    private DoctorDrugWarehousingIntermediateMapper doctorDrugWarehousingIntermediateMapper;
    @Autowired
    private DockerDrugMapper dockerDrugMapper;


    /**
     * 查询药品入库
     * 
     * @param id 药品入库主键
     * @return 药品入库
     */
    @Override
    public DoctorWarehousing selectDoctorWarehousingById(Long id)
    {
        return doctorWarehousingMapper.selectDoctorWarehousingById(id);
    }

    /**
     * 查询药品入库列表
     * 
     * @param doctorWarehousing 药品入库
     * @return 药品入库
     */
    @Override
    public List<DoctorWarehousing> selectDoctorWarehousingList(DoctorWarehousing doctorWarehousing)
    {
        return doctorWarehousingMapper.selectDoctorWarehousingList(doctorWarehousing);
    }

    /**
     * 新增药品入库
     * 
     * @param doctorWarehousing 药品入库
     * @return 结果
     */
    @Override
    public int insertDoctorWarehousing(DoctorWarehousing doctorWarehousing)
    {
        return doctorWarehousingMapper.insertDoctorWarehousing(doctorWarehousing);
    }

    /**
     * 修改药品入库
     * 
     * @param doctorWarehousing 药品入库
     * @return 结果
     */
    @Override
    public int updateDoctorWarehousing(DoctorWarehousing doctorWarehousing)
    {

//        System.out.println(doctorWarehousing);
//        System.out.println(doctorWarehousing.getAuditStatus());

        if(doctorWarehousing.getAuditStatus().equals("1")){

            String warehousingOrderId = doctorWarehousing.getWarehousingOrderId();
            //   根据订单号查询入库中间表查询
            List<DoctorDrugWarehousingIntermediate> list = doctorDrugWarehousingIntermediateMapper.selectWarehousingOrderId(warehousingOrderId);

//            循环list 获取药品编码 查询药品表
            for (DoctorDrugWarehousingIntermediate doctorDrugWarehousingIntermediate : list) {
                String drugCode = doctorDrugWarehousingIntermediate.getDrugCode();
//                获取药品数量
                Long quantity = doctorDrugWarehousingIntermediate.getQuantity();
                //   根据药品编码查询药品表 将库存加quantity
                DockerDrug dockerDrug = dockerDrugMapper.selectDockerDrugByCodes(drugCode);
                dockerDrug.setDrugInventory(dockerDrug.getDrugInventory()+quantity);
//                修改药品库存
                dockerDrugMapper.updateDockerDrug(dockerDrug);
            }

        }

        return doctorWarehousingMapper.updateDoctorWarehousing(doctorWarehousing);
    }

    /**
     * 批量删除药品入库
     * 
     * @param ids 需要删除的药品入库主键
     * @return 结果
     */
    @Override
    public int deleteDoctorWarehousingByIds(Long[] ids)
    {
        return doctorWarehousingMapper.deleteDoctorWarehousingByIds(ids);
    }

    /**
     * 删除药品入库信息
     * 
     * @param id 药品入库主键
     * @return 结果
     */
    @Override
    public int deleteDoctorWarehousingById(Long id)
    {
        return doctorWarehousingMapper.deleteDoctorWarehousingById(id);
    }
}
